<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/


namespace Member\Model;
use Think\Model;
use \Org\Util\String;
    
class MemberAddressModel extends MemberModel {

	//protected $insertFields = array('username','usertype','typename','password','powercode','usergroup','groupname','email','unionid','uniontime','regip','dateline','mobile');
	
	//protected $updateFields = array('avatar','SecurityForm');
	
	//字段过滤-----------------------------------------------------------------------------------------------
	//protected $insertFields = array('title','status','rules','icon','color','allowdomainname','allowlogin','allowskillnumber','allowsendsms','feetype','taskfee','SecurityForm');
	//protected $updateFields = array('usertype','typename','password','powercode','usergroup','groupname','email','unionid','uniontime','regip','dateline','mobile','birthday','about','gender','area','phone','qq','provid','cityid','distid','provname','cityname','distname','address','SecurityForm');
	
	
	//字段检查-----------------------------------------------------------------------------------------------
	protected $_validate = array(
		//array('title','require','请输入名称！'),
	);
	
	//数据过滤-----------------------------------------------------------------------------------------------
	function FormatData(){

		$data['uid']=$this->uid;
		$data['address']=String::msubstr(I('post.address','','string'),0,200,C('S_CHARSET'),false);
		$data['receivename']=String::msubstr(I('post.receivename','','string'),0,30,C('S_CHARSET'),false);
		$data['phone']=IsNum($_POST['phone']) ? String::msubstr($_POST['phone'],0,16,C('S_CHARSET'),false) : 0;
		$data['zip']=IsNum($_POST['zip']) ? String::msubstr($_POST['zip'],0,6,C('S_CHARSET'),false) : 0;
		$data['isuse']= intval($_POST['isuse'])==1 ? 1 : 2;
		
		
		//所在地---------------------------------------------------------------------------------
		$provid=$data['provid']=intval($_POST['provid']);
		
		$cityid=$data['cityid']=intval($_POST['cityid']);
		
		$distid=$data['distid']=intval($_POST['distid']);
		
		if($provid && $cityid && $distid){
		
			$provnamearr=M('CategoryCity')->where("catid=$provid")->find();
			
			$citynamearr=M('CategoryCity')->where("catid=$cityid")->find();
			
			$distnamearr=M('CategoryCity')->where("catid=$distid")->find();
			
			$data['provname']=$provnamearr['name'];
			
			$data['cityname']=$citynamearr['name'];
			
			$data['distname']=$distnamearr['name'];
		
		}
			
		$data['SecurityForm']=I('post.SecurityForm','','string');
		return $data;
	}
	
	function UpdateDefaultData($id=0){
		$data=$this->FormatData();
		if(!$id){
			$id=intval($_POST['id']);
		}
		if($data['isuse']==1 && $id>0){
			$this->isuse=0;
			if($this->where(" uid=$this->uid AND id!=$id ")->save()){
				return true;
			}
			$this->error=$this->getError();
			return false;
		}
	}
	
	
	function CheckDefaultData(){
		$data=$this->FormatData();
		$id=intval($_POST['id']);
		//修改
		if($data['isuse']!=1 && $id>0){
			return $this->where(" uid=$this->uid AND isuse=1 AND id!=$id ")->count();
		}
		
		//插入
		if($data['isuse']!=1 && !$id){
			return $this->where(" uid=$this->uid AND isuse=1")->count();
		}
		
		return true;
	
	}
	
}

?>